package cn.itcast.service.impl;

import cn.itcast.dao.TransitRepository;
import cn.itcast.dao.WayBillRepository;
import cn.itcast.domain.delivery.WayBill;
import cn.itcast.domain.transit.TransitInfo;
import cn.itcast.index.WayBillIndexRepository;
import cn.itcast.service.TransitService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class TransitServiceImpl implements TransitService {
    @Autowired
    private TransitRepository transitRepository;
    @Autowired
    private WayBillRepository wayBillRepository;
    @Autowired
    private WayBillIndexRepository wayBillIndexRepository;

    @Override
    public void createTransits(String wayBillIds) {
        //数据非空判断
        if (StringUtils.isNotBlank(wayBillIds)) {
            for (String wayBillId : wayBillIds.split(",")) {
                WayBill wayBill = wayBillRepository.findOne(Integer.parseInt(wayBillId));

                if (wayBill.getSignStatus()!=2) {
                    TransitInfo transitInfo = new TransitInfo();
                    transitInfo.setWayBill(wayBill);
                    transitInfo.setStatus("出入库中转");
                    transitRepository.save(transitInfo);
                    wayBill.setSignStatus(2);
                    //同步数据到索引库
                    wayBillRepository.save(wayBill);
                }

            }

        }
    }

    @Override
    public Page<TransitInfo> findPageData(Pageable pageable) {

        return transitRepository.findAll(pageable);
    }
}
